package com.cmcc.aregister.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.constant.GlobalSetting;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.model.Department;
import com.cmcc.aregister.cs.model.Disease;
import com.cmcc.aregister.cs.model.Doctor;
import com.cmcc.aregister.cs.model.Hospital;
import com.cmcc.aregister.cs.util.PotoalParcelable;
import com.cmcc.aregister.cs.util.Utils;
import com.cmcc.aregister.ui.adapter.DepartmentAdapter;
import com.cmcc.aregister.ui.adapter.DiseaseAdapter;
import com.cmcc.aregister.ui.adapter.DiseaseTypeAdapter;
import com.cmcc.aregister.ui.adapter.DoctorAdapter;
import com.cmcc.aregister.ui.adapter.HospitalAdapter;

/**
 * 搜索页面
 */
public class SearchActivity extends ParentActivity implements OnClickListener {
    private static final String TAG = "SearchActivity";
    // ------view------
    private Button top_left_image;
    private EditText search_content;
    private ImageView search_btn;
    private TextView search_type;
    private ListView search_list;
    private LinearLayout no_data_layout;

    // ------parameter------
    private List<Hospital> hospitalList = new ArrayList<Hospital>();
    private List<Doctor> doctorList = new ArrayList<Doctor>();
    private List<Department> departmentList = new ArrayList<Department>();
    private List<Disease> diseaseList = new ArrayList<Disease>();
    private List<Disease> diseaseTypeList = new ArrayList<Disease>();
    private int currPage = 0;
    private int allPage = 0;
    private int currSelectType = 0;
    private String search_content_str = "";

    // ------view------
    private HospitalAdapter hospitalAdapter;
    private DepartmentAdapter departmentAdapter;
    private DoctorAdapter doctorAdapter;
    private DiseaseAdapter diseaseAdapter;
    private DiseaseTypeAdapter diseaseTypeAdapter;
    private String catNo = "";
    private int hasDeaseList = 0;
    //
    private String[] searchArrays;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(R.layout.p_search);
        mHttpManager.setCxt(getParent());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        searchArrays = null;
    }

    @Override
    protected void setupData() {
        searchArrays = getResources().getStringArray(R.array.search_key);
    }

    @Override
    protected void setupView() {
        ((ImageView) findViewById(R.id.title))
            .setBackgroundResource(R.drawable.search_title);
        // ((TextView) findViewById(R.id.title)).setText(R.string.search);
        top_left_image = (Button) findViewById(R.id.top_left_image);
        search_content = (EditText) findViewById(R.id.search_content);
        search_btn = (ImageView) findViewById(R.id.search_btn);
        search_type = (TextView) findViewById(R.id.search_type);
        search_type.setOnClickListener(this);
        search_list = (ListView) findViewById(R.id.search_list);
        no_data_layout = (LinearLayout) findViewById(R.id.no_data_layout);

        search_content.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    search();
                    return true;
                }
                else if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search();
                    return true;
                }
                return false;
            }
        });

        search_content.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                if (search_content.getText().toString().equals("")) {
                    search_btn.setImageResource(R.drawable.search_btn_bg);
                    search();
                }
                else {
                    search_btn.setImageResource(R.drawable.search_btn_bg_lv);
                }
            }
        });

        top_left_image.setVisibility(View.VISIBLE);
        top_left_image.setOnClickListener(onClickListener);
        search_btn.setOnClickListener(onClickListener);
        search_list.setOnItemClickListener(onItemClickListener);
        // String[] arr = getResources().getStringArray(R.array.search_key);
        // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        // android.R.layout.simple_spinner_item, arr);
        /*
         * ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
         * this, R.array.search_key, android.R.layout.simple_spinner_item);
         */
        // adapter
        // .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // search_type.setAdapter(adapter);
        // search_type.setOnItemSelectedListener(onItemSelectedListener);
        // search_type.setSelection(3, true);
        //默认显示疾病
        diseaseTypeList = new ArrayList<Disease>();
        search_content_str = "";
        currSelectType=3;
        search_content.setText(search_content_str);
        sendGetDiseaseTypeListTask(search_content_str);
    }

    OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
            Intent intent = null;
            Map<String, Object> httpMap = new HashMap<String, Object>();
            PotoalParcelable potoal = new PotoalParcelable(httpMap);
            switch (currSelectType) {
            case 0:
                intent = new Intent(SearchActivity.this,
                    HospitalDetailActivity.class);
                httpMap.put(Arguments.ACTIVITY_JUMP_HOSPITAL,
                    hospitalList.get(position));
                httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE,
                    hospitalList.get(position).area_type);
                httpMap.put(Arguments.ACTIVITY_JUMP_POSITION, position);
                intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 1:
                intent = new Intent(SearchActivity.this,
                    DepartmentDetailActivity.class);
                httpMap.put(Arguments.ACTIVITY_JUMP_DEPARTMENT,
                    departmentList.get(position));
                httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE,
                    departmentList.get(position).area_type);
                httpMap.put(Arguments.ACTIVITY_JUMP_POSITION, position);
                intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 2:
                intent = new Intent(SearchActivity.this,
                    DoctorDetailActivity.class);
                httpMap.put(Arguments.ACTIVITY_JUMP_DOCTOR,
                    doctorList.get(position));
                httpMap.put(Arguments.ACTIVITY_JUMP_AREA_TYPE,
                    doctorList.get(position).area_type);
                httpMap.put(Arguments.ACTIVITY_JUMP_POSITION, position);
                intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 3:
                if (hasDeaseList == 0) {
                    top_left_image
                        .setBackgroundResource(R.drawable.back_selecter);
                    catNo = diseaseTypeList.get(position).cat_no;
                    search_content_str = "";
                    search_content.setText(search_content_str);
                    sendGetDiseaseListTask(search_content_str, "1", catNo,
                        HttpAsyncTask.CACHE_TYPE_SAVE);
                    diseaseTypeList.clear();
                    hasDeaseList = 1;
                }
                else {
                    intent = new Intent(SearchActivity.this,
                        DiseaseDetailActivity.class);
                    httpMap.put(Arguments.ACTIVITY_JUMP_DISEASE,
                        diseaseList.get(position));
                    httpMap.put(Arguments.ACTIVITY_JUMP_POSITION, position);
                    intent.putExtra(Arguments.INTENT_TO_NEXT_PAGE, potoal);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
                break;
            }
        }
    };

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.top_left_image) {
                if (hasDeaseList == 1) {
                    top_left_image
                        .setBackgroundResource(R.drawable.home_drawable);
                    hasDeaseList = 0;
                    // 返回上一级时，情况输入框查询内容。
                    search_content_str = "";
                    search_content.setText(search_content_str);
                    diseaseTypeList = new ArrayList<Disease>();
                    sendGetDiseaseTypeListTask(search_content_str);
                }
                else {
                    Intent intent = new Intent(SearchActivity.this,
                        NewIndexActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }
            else if (id == R.id.search_btn) {
                search();
            }
        }
    };

    public void search() {
        try {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
        catch (Exception e) {

        }
        search_content_str = search_content.getText().toString();

        // CmccDataStatistics.SMZY_SearchKeyword(this, search_str);
        switch (currSelectType) {
        case 0:
            hospitalList = new ArrayList<Hospital>();
            sendSearchHospitalTask("2", "0", "", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            break;
        case 1:
            departmentList = new ArrayList<Department>();
            sendSearchDepartmentTask("2", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            break;
        case 2:
            doctorList = new ArrayList<Doctor>();
            sendSearchDoctorTask("2", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            break;
        case 3:
            if (hasDeaseList == 0) {
                diseaseTypeList = new ArrayList<Disease>();
                sendGetDiseaseTypeListTask(search_content_str);
            }
            else {
                diseaseList = new ArrayList<Disease>();
                sendGetDiseaseListTask(search_content_str, "1", catNo,
                    HttpAsyncTask.CACHE_TYPE_SAVE);
            }
            break;
        }
    }

    OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
            int position, long arg3) {
            TextView tView = (TextView) view;
            tView.setTextColor(Color.BLACK);
            currSelectType = position;
            // search_list.setAdapter(null);
            search_content_str = search_content.getText().toString();
            top_left_image.setBackgroundResource(R.drawable.home_drawable);
            switch (position) {
            case 0:
                // if (hospitalAdapter == null) {
                hospitalList = new ArrayList<Hospital>();
                sendSearchHospitalTask("2", "0", "", search_content_str, "1",
                    HttpAsyncTask.CACHE_TYPE_SAVE);
                // } else {
                // search_list.setAdapter(hospitalAdapter);
                // }
                break;
            case 1:
                // if (departmentAdapter == null) {
                departmentList = new ArrayList<Department>();
                sendSearchDepartmentTask("2", search_content_str, "1",
                    HttpAsyncTask.CACHE_TYPE_SAVE);
                // } else {
                // search_list.setAdapter(departmentAdapter);
                // }
                break;
            case 2:
                // if (doctorAdapter == null) {
                doctorList = new ArrayList<Doctor>();
                sendSearchDoctorTask("2", search_content_str, "1",
                    HttpAsyncTask.CACHE_TYPE_SAVE);
                // } else {
                // search_list.setAdapter(doctorAdapter);
                // }
                break;
            case 3:
                // if (diseaseAdapter == null) {
                if (hasDeaseList == 1) {
                    top_left_image
                        .setBackgroundResource(R.drawable.back_selecter);
                }
                diseaseTypeList = new ArrayList<Disease>();
                sendGetDiseaseTypeListTask(search_content_str);
                // } else {
                // search_list.setAdapter(diseaseAdapter);
                // }
                break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    };

    public void setView() {
        switch (currSelectType) {
        case 0:
            if (hospitalList.size() > 0) {
                if (hospitalAdapter == null) {
                    hospitalAdapter = new HospitalAdapter(context,
                        hospitalList, handler, currPage, allPage, false);
                    search_list.setAdapter(hospitalAdapter);
                }
                else {
                    hospitalAdapter.list = hospitalList;
                    hospitalAdapter.currPage = currPage;
                    hospitalAdapter.allPage = allPage;
                    hospitalAdapter.notifyDataSetChanged();
                    if (search_list.getAdapter() != hospitalAdapter) {
                        search_list.setAdapter(hospitalAdapter);
                    }
                }
                showNoData(false);
            }
            else {
                showNoData(true);
            }
            break;
        case 1:
            if (departmentList.size() > 0) {
                if (departmentAdapter == null) {
                    departmentAdapter = new DepartmentAdapter(context,
                        departmentList, handler, currPage, allPage, false);
                    search_list.setAdapter(departmentAdapter);
                }
                else {
                    departmentAdapter.list = departmentList;
                    departmentAdapter.currPage = currPage;
                    departmentAdapter.allPage = allPage;
                    departmentAdapter.notifyDataSetChanged();
                    if (search_list.getAdapter() != departmentAdapter) {
                        search_list.setAdapter(departmentAdapter);
                    }
                }
                showNoData(false);
            }
            else {
                showNoData(true);
            }
            break;
        case 2:
            if (doctorList.size() > 0) {
                if (doctorAdapter == null) {
                    doctorAdapter = new DoctorAdapter(context, doctorList,
                        handler, currPage, allPage, false);
                    search_list.setAdapter(doctorAdapter);
                }
                else {
                    doctorAdapter.list = doctorList;
                    doctorAdapter.currPage = currPage;
                    doctorAdapter.allPage = allPage;
                    doctorAdapter.notifyDataSetChanged();
                    if (search_list.getAdapter() != doctorAdapter) {
                        search_list.setAdapter(doctorAdapter);
                    }
                }
                showNoData(false);
            }
            else {
                showNoData(true);
            }
            break;
        case 3:
            if (diseaseTypeList.size() > 0 && hasDeaseList == 0) {
                if (diseaseTypeAdapter == null) {
                    diseaseTypeAdapter = new DiseaseTypeAdapter(context,
                        diseaseTypeList);
                    search_list.setAdapter(diseaseTypeAdapter);
                }
                else {
                    diseaseTypeAdapter.list = diseaseTypeList;
                    diseaseTypeAdapter.notifyDataSetChanged();
                    if (search_list.getAdapter() != diseaseTypeAdapter) {
                        search_list.setAdapter(diseaseTypeAdapter);
                    }
                }
                showNoData(false);
            }
            else if (diseaseList.size() > 0 && hasDeaseList == 1) {
                if (diseaseAdapter == null) {
                    diseaseAdapter = new DiseaseAdapter(context, diseaseList,
                        handler, currPage, allPage);
                    search_list.setAdapter(diseaseAdapter);
                }
                else {
                    diseaseAdapter.list = diseaseList;
                    diseaseAdapter.currPage = currPage;
                    diseaseAdapter.allPage = allPage;
                    diseaseAdapter.notifyDataSetChanged();
                    if (search_list.getAdapter() != diseaseAdapter) {
                        search_list.setAdapter(diseaseAdapter);
                    }
                }
                showNoData(false);
            }
            else {
                showNoData(true);
            }
            break;
        }
    }

    /**
     * 是否显示无数据图片
     * 
     * @param flag
     */
    public void showNoData(boolean flag) {
        if (flag) {
            search_list.setVisibility(View.GONE);
            no_data_layout.setVisibility(View.VISIBLE);
        }
        else {
            search_list.setVisibility(View.VISIBLE);
            no_data_layout.setVisibility(View.GONE);
        }
    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (currSelectType) {
            case 0:
                sendSearchHospitalTask("2", "0", "", search_content_str,
                    currPage + 1 + "", HttpAsyncTask.CACHE_TYPE_ACTION);
                break;
            case 1:
                sendSearchDepartmentTask("2", search_content_str, currPage + 1
                    + "", HttpAsyncTask.CACHE_TYPE_ACTION);
                break;
            case 2:
                sendSearchDoctorTask("2", search_content_str,
                    currPage + 1 + "", HttpAsyncTask.CACHE_TYPE_ACTION);
                break;
            case 3:
                if (hasDeaseList == 1) {
                    sendGetDiseaseListTask(search_content_str, currPage + 1
                        + "", catNo, HttpAsyncTask.CACHE_TYPE_ACTION);
                }
                break;
            }
        }
    };

    /**
     * 获取疾病分类
     */
    private HttpAsyncTask sendGetDiseaseTypeListTask;

    private void sendGetDiseaseTypeListTask(String search_content) {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("search_content", search_content);
        String getDiseaseTypeListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DISEASE_TYPE_LIST, parameterMap);
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "查询疾病分类：" + getDiseaseTypeListUrl);
        }
        showProgressDialog();
        sendGetDiseaseTypeListTask = new HttpAsyncTask(HttpAsyncTask.GET,
            GlobalSetting.XML_GET_DISEASE_TYPE_LIST, getDiseaseTypeListUrl,
            HttpAsyncTask.CACHE_TYPE_ACTION, true);
        sendGetDiseaseTypeListTask
            .setUIHandler(sendGetDiseaseTypeListTaskHandler);
        mHttpManager.execute(sendGetDiseaseTypeListTask);
    }

    @SuppressWarnings("unchecked")
    Handler sendGetDiseaseTypeListTaskHandler = new Handler() {
        public void handleMessage(Message msg) {
            logger.d("----------sendGetDiseaseTypeListTaskHandler----------");
            switch (msg.what) {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null) {
                    try {
                        Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
                        List<Disease> list = (List<Disease>) httpMap
                            .get(Arguments.XML_DISEASE_LIST);
                        diseaseTypeList = list;
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.search_fail),
                        getString(R.string.close));
                    diseaseTypeList = new ArrayList<Disease>();
                }
                setView();
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.read_fail), getString(R.string.close));
                setView();
                break;
            }
        }
    };

    /**
     * 获取疾病列表
     */
    private HttpAsyncTask sendGetDiseaseListTask;

    /**
     * 
     * @param search_content 查询内容 Y 为空时查询所有
     * @param page 页数 N 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 11 Y 默认为10
     */
    private void sendGetDiseaseListTask(String search_content, String page,
        String catNo, int cacheType) {
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("search_content", search_content);
        parameterMap.put("cat_no", catNo);
        parameterMap.put("page", page);
        // parameterMap.put("pageSize", "15");
        String getDiseaseListUrl = Utils.changeUrl(
            Arguments.SERVER_URL_GET_DISEASE_LIST, parameterMap);
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "查询疾病列表：" + getDiseaseListUrl);
        }
        if (cacheType == HttpAsyncTask.CACHE_TYPE_ACTION) {
            sendGetDiseaseListTask = new HttpAsyncTask(HttpAsyncTask.GET,
                GlobalSetting.XML_GET_DISEASE_LIST, getDiseaseListUrl,
                cacheType, true);
        }
        else {
            sendGetDiseaseListTask = new HttpAsyncTask(HttpAsyncTask.GET,
                GlobalSetting.XML_GET_DISEASE_LIST, getDiseaseListUrl,
                cacheType, false);
        }
        sendGetDiseaseListTask.setUIHandler(sendGetDiseaseListTaskHandler);
        mHttpManager.execute(sendGetDiseaseListTask);
    }

    @SuppressWarnings("unchecked")
    Handler sendGetDiseaseListTaskHandler = new Handler() {
        public void handleMessage(Message msg) {
            logger.d("----------sendGetDiseaseListTaskHandler----------");
            switch (msg.what) {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null) {
                    try {
                        Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
                        allPage = Integer.parseInt((String) httpMap
                            .get(Arguments.XML_ALLPAGE));
                        currPage = Integer.parseInt((String) httpMap
                            .get(Arguments.XML_CURRPAGE));
                        List<Disease> diseaseList_1 = (List<Disease>) httpMap
                            .get(Arguments.XML_DISEASE_LIST);
                        if (currPage == 1) {
                            diseaseList = diseaseList_1;
                        }
                        else {
                            diseaseList.addAll(diseaseList_1);
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        allPage = currPage;
                    }
                }
                else {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.search_fail),
                        getString(R.string.close));
                    diseaseList = new ArrayList<Disease>();
                }
                setView();
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.read_fail), getString(R.string.close));
                allPage = currPage;
                setView();
                break;
            }
        }
    };

    /**
     * 查询名医名院
     */
    private HttpAsyncTask sendSearchTask;

    /**
     * @param area_type 区域类别 N 0：本地 1：异地 2：所有
     * @param search_type 查询类别 N 0:搜索医院1:搜索科室2:搜索医生
     * @param area_id 医院区域 Y 本地: 7罗湖区，3317福田区，3318盐田区，3319龙岗区，8南山区，3320宝安区，3321
     *        光明新区； 异地： 1 广州，6北京，7上海，9武汉，5长沙，10成都，11西安，8重庆
     * @param unit_level 医院级别 Y
     * @param cat_no 科室分类 Y
     *        A内科；B外科；C肿瘤科；D妇产科；E儿科；F五官科；G男科；H传染科；I皮肤性病；J精神心理科K中西医结合科
     *        L生殖健康M急诊科N老年科
     * @param search_content 查询内容 Y
     * @param page 页数 N 0 表示不分页为所有，分页从1开始
     * @param pageSize 每页记录数 11 Y 默认为10
     */
    private void sendSearchTask(Map<String, String> parameterMap, int cache_type) {
        String searchUrl = Utils.changeUrl(
            Arguments.SERVER_URL_SEARCH_FRMOUS_HOSPITAL, parameterMap);
        if (ConfigUtilHelper.debug) {
            Log.i(TAG, "查询名医名院：" + searchUrl);
        }
        if (cache_type == HttpAsyncTask.CACHE_TYPE_ACTION) {
            sendSearchTask = new HttpAsyncTask(HttpAsyncTask.GET,
                GlobalSetting.XML_SEARCH, searchUrl, cache_type, true);
        }
        else {
            sendSearchTask = new HttpAsyncTask(HttpAsyncTask.GET,
                GlobalSetting.XML_SEARCH, searchUrl, cache_type, false);
        }
        sendSearchTask.setUIHandler(sendSearchTaskHandler);
        mHttpManager.execute(sendSearchTask);
    }

    private void sendSearchHospitalTask(String area_type, String area_id,
        String unit_level, String search_content, String page, int cache_type) {
        logger.d("----------sendSearchHospitalTask----------" + page);
        area_type = "0";
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("search_type", "0");
        parameterMap.put("area_id", area_id);
        parameterMap.put("unit_level", unit_level);
        parameterMap.put("search_content", search_content);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", Arguments.pageSize);
        parameterMap.put("mobile", ConfigUtilHelper.getMobile(app));
        sendSearchTask(parameterMap, cache_type);
    }

    @SuppressWarnings("unchecked")
    Handler sendSearchTaskHandler = new Handler() {
        public void handleMessage(Message msg) {
            logger.d("----------sendSearchTaskHandler----------");
            switch (msg.what) {
            case HttpAsyncTask.TASK_NOTIFY_RETURN_DATA:
                dismissProgressDialog();
                if (msg.obj != null) {
                    try {
                        Map<String, Object> httpMap = (Map<String, Object>) msg.obj;
                        allPage = Integer.parseInt((String) httpMap
                            .get(Arguments.XML_ALLPAGE));
                        currPage = Integer.parseInt((String) httpMap
                            .get(Arguments.XML_CURRPAGE));
                        switch (currSelectType) {
                        case 0:
                            List<Hospital> hospitalList_1 = (List<Hospital>) httpMap
                                .get(Arguments.XML_HOSPITAL_LIST);
                            if (currPage == 1) {
                                hospitalList = hospitalList_1;
                            }
                            else {
                                hospitalList.addAll(hospitalList_1);
                            }
                            break;
                        case 1:
                            List<Department> departmentList_1 = (List<Department>) httpMap
                                .get(Arguments.XML_DEPARTMENT_LIST);
                            if (currPage == 1) {
                                departmentList = departmentList_1;
                            }
                            else {
                                departmentList.addAll(departmentList_1);
                            }
                            break;
                        case 2:
                            List<Doctor> doctorList_1 = (List<Doctor>) httpMap
                                .get(Arguments.XML_DOCTOR_LIST);
                            if (currPage == 1) {
                                doctorList = doctorList_1;
                            }
                            else {
                                doctorList.addAll(doctorList_1);
                            }
                            break;
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        allPage = currPage;
                    }
                }
                else {
                    showSingleAlertDlg(getString(R.string.notice),
                        getString(R.string.search_fail),
                        getString(R.string.close));
                    switch (currSelectType) {
                    case 0:
                        hospitalList = new ArrayList<Hospital>();
                        break;
                    case 1:
                        departmentList = new ArrayList<Department>();
                        break;
                    case 2:
                        doctorList = new ArrayList<Doctor>();
                        break;
                    }
                }
                setView();
                break;
            case HttpAsyncTask.TASK_NOTIFY_CONNECTION_FAIL:
                dismissProgressDialog();
                showSingleAlertDlg(getString(R.string.notice),
                    getString(R.string.read_fail), getString(R.string.close));
                allPage = currPage;
                setView();
                break;
            }
        }
    };

    private void sendSearchDepartmentTask(String area_type,
        String search_content, String page, int cache_type) {
        area_type = "0";
        logger.d("----------sendSearchDepartmentTask----------" + page);
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("search_type", "1");
        parameterMap.put("search_content", search_content);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", Arguments.pageSize);
        parameterMap.put("mobile", ConfigUtilHelper.getMobile(app));
        sendSearchTask(parameterMap, cache_type);
    }

    private void sendSearchDoctorTask(String area_type, String search_content,
        String page, int cache_type) {
        area_type = "0";
        logger.d("----------sendSearchDepartmentTask----------" + page);
        Map<String, String> parameterMap = new HashMap<String, String>();
        parameterMap.put("area_type", area_type);
        parameterMap.put("search_type", "2");
        parameterMap.put("search_content", search_content);
        parameterMap.put("page", page);
        parameterMap.put("pageSize", Arguments.pageSize);
        parameterMap.put("mobile", ConfigUtilHelper.getMobile(app));
        sendSearchTask(parameterMap, cache_type);
    }

    @Override
    protected void setTaskHandlerNull() {
        if (sendSearchTask != null) {
            sendSearchTask.setTaskBindingHandler(null);
        }
        if (sendGetDiseaseListTask != null) {
            sendGetDiseaseListTask.setTaskBindingHandler(null);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (app.changeAttention) {
            switch (app.type) {
            case 0:
                if (hospitalAdapter != null) {
                    Hospital hospital = hospitalList.get(app.position);
                    hospital.is_attention = app.attentioin;
                    hospitalList.set(app.position, hospital);
                    hospitalAdapter.list = hospitalList;
                    hospitalAdapter.notifyDataSetChanged();
                }
                break;
            case 1:
                if (departmentAdapter != null) {
                    Department department = departmentList.get(app.position);
                    department.is_attention = app.attentioin;
                    departmentList.set(app.position, department);
                    departmentAdapter.list = departmentList;
                    departmentAdapter.notifyDataSetChanged();
                }
                break;
            case 2:
                if (doctorAdapter != null) {
                    Doctor doctor = doctorList.get(app.position);
                    doctor.is_attention = app.attentioin;
                    doctorList.set(app.position, doctor);
                    doctorAdapter.list = doctorList;
                    doctorAdapter.notifyDataSetChanged();
                }
                break;
            }
            app.changeAttention = false;
            app.position = -1;
            app.type = -1;
            app.attentioin = "";
        }
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.search_type:
            new AlertDialog.Builder(this).setItems(searchArrays,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        onSelectSearchType(arg1);
                    }
                }).show();
            break;
        }
    }

    private void onSelectSearchType(int position) {
        // TextView tView = (TextView) view;
        // tView.setTextColor(Color.BLACK);
        search_type.setText(searchArrays[position]);
        currSelectType = position;
        // search_list.setAdapter(null);
        search_content_str = search_content.getText().toString();
        top_left_image.setBackgroundResource(R.drawable.home_drawable);
        switch (position) {
        case 0:
            // if (hospitalAdapter == null) {
            hospitalList = new ArrayList<Hospital>();
            sendSearchHospitalTask("2", "0", "", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            // } else {
            // search_list.setAdapter(hospitalAdapter);
            // }
            break;
        case 1:
            // if (departmentAdapter == null) {
            departmentList = new ArrayList<Department>();
            sendSearchDepartmentTask("2", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            // } else {
            // search_list.setAdapter(departmentAdapter);
            // }
            break;
        case 2:
            // if (doctorAdapter == null) {
            doctorList = new ArrayList<Doctor>();
            sendSearchDoctorTask("2", search_content_str, "1",
                HttpAsyncTask.CACHE_TYPE_SAVE);
            // } else {
            // search_list.setAdapter(doctorAdapter);
            // }
            break;
        case 3:
            // if (diseaseAdapter == null) {
            if (hasDeaseList == 1) {
                top_left_image.setBackgroundResource(R.drawable.back_selecter);
            }
            diseaseTypeList = new ArrayList<Disease>();
            sendGetDiseaseTypeListTask(search_content_str);
            // } else {
            // search_list.setAdapter(diseaseAdapter);
            // }
            break;
        }
    }
}